package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.MasterCreator;
import com.hotel.BL.PayTypeLogic;

import data.PayModeMaster;
import data.PayType;
import data.passingData.PayModeAndPayType;
import data.passingData.PayTypeData;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class PayTypeService extends HttpServlet{	
	//http://localhost:8080/paytypeService?func=addPayType&hotelId=1&title=1&shortname=1&mode=1&description=aa
	//http://localhost:8080/paytypeService?func=editPayType&&paytypeId=865&title=222222222222222&shortname=1&mode=1&description=aaaaa
	//http://localhost:8080/paytypeService?func=delPayType&paytypeId=865
	//http://localhost:8080/paytypeService?func=changePayType&paytypeId=865
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addPayType")){
				AddPayType(req,resp);
			}else if(doClass.equals("editPayType")){
				EditPayType(req,resp);
			}else if(doClass.equals("delPayType")){
				DeletePayType(req,resp);
			}else if(doClass.equals("changePayType")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getPayType")){
				GetPayType(req,resp);
			}else if(doClass.equals("GetAllPayModeAndPayType")){
				GetAllPayModeAndPayType(req,resp);
			}else if(doClass.equals("GetAllPayType4Admin")){
				GetAllPayType4Admin(req,resp);
			}else if(doClass.equals("GetPayMode")){
				GetPayMode(req,resp);
			}else if(doClass.equals("getPayTypeById")){
				GetPayTypeById(req,resp);
			}else if(doClass.equals("getAllPayType")){
				GetAllPayType(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddPayType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		long mode = Long.parseLong(req.getParameter("mode"));
		String description = req.getParameter("description");

		new PayTypeLogic().AddPayType(hotelId,title,mode,description);

		GetAllPayType4Admin(req, resp);
	}

	private void EditPayType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long paytypeId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		long mode = Long.parseLong(req.getParameter("mode"));
		String description = req.getParameter("description");
				
		new PayTypeLogic().EditPayType(paytypeId,title,mode,description);

		GetAllPayType4Admin(req, resp);
	}

	private void DeletePayType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String roomIdList = req.getParameter("id");
		
		for(int i = 0; i < roomIdList.split(",").length ; i++){
			new PayTypeLogic().DeletePayType(Long.parseLong(roomIdList.split(",")[i]));
		}
		
		GetAllPayType4Admin(req, resp);
	}
	
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long paytypeId = Long.parseLong(req.getParameter("paytypeId"));

		Object obj = new PayTypeLogic().ChangeStatus(paytypeId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetPayType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String mode = req.getParameter("mode");
		Object obj = new PayTypeLogic().GetPayType(hotelId,mode);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllPayModeAndPayType(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		List<PayType> paytype = new PayTypeLogic().GetAllPayType(hotelId);
		List<PayModeMaster> paymode = new MasterCreator().GetAllPayMode();
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(new PayModeAndPayType(paymode,paytype)));
	}
	
	private void GetAllPayType4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<PayTypeData> obj = new PayTypeLogic().GetAllPayType4AdminLimit(hotelId,offset,line);
		double dataSize = new PayTypeLogic().GetAllPayType4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}

	private void GetPayMode(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		List<PayModeMaster> paymode = new MasterCreator().GetAllPayMode();
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(paymode));
	}
	
	private void GetPayTypeById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		PayType paytype = new PayTypeLogic().GetPayType(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(paytype));
	}
	
	private void GetAllPayType(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		
		List<PayType> list = new PayTypeLogic().GetAllPayType(hotelId);
		
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(list));
	}
}